<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/sure3.css" rel="stylesheet" type="text/css"/>
    <link href="../Styles/base1.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<div class="markdown-body entry-content" itemprop="text">
    <h1><a href="#数值的扩展"  class="anchor" id="user-content-数值的扩展">

    </a>数值的扩展
    </h1>
    <h2><a href="#二进制和八进制表示法"  class="anchor" id="user-content-二进制和八进制表示法">

    </a>二进制和八进制表示法
    </h2>
    <p>ES6 提供了二进制和八进制数值的新的写法，分别用前缀<code>0b</code>（或<code>0B</code>）和<code>0o</code>（或<code>0O</code>）表示。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">0b111110111</span> <span class="pl-k">===</span> <span
            class="pl-c1">503</span> <span class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">0o767</span> <span class="pl-k">===</span> <span class="pl-c1">503</span> <span class="pl-c"><span
                class="pl-c">//</span> true</span></pre>
    </div>
    <p>从 ES5 开始，在严格模式之中，八进制就不再允许使用前缀<code>0</code>表示，ES6 进一步明确，要使用前缀<code>0o</code>表示。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c"><span class="pl-c">//</span> 非严格模式</span>
(<span class="pl-k">function</span>(){
  <span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span class="pl-c1">0o11</span> <span class="pl-k">===</span> <span
                class="pl-c1">011</span>);
})() <span class="pl-c"><span class="pl-c">//</span> true</span>

<span class="pl-c"><span class="pl-c">//</span> 严格模式</span>
(<span class="pl-k">function</span>(){
  <span class="pl-s"><span class="pl-pds">'</span>use strict<span class="pl-pds">'</span></span>;
  <span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span class="pl-c1">0o11</span> <span class="pl-k">===</span> <span
                class="pl-c1">011</span>);
})() <span class="pl-c"><span class="pl-c">//</span> Uncaught SyntaxError: Octal literals are not allowed in strict mode.</span></pre>
    </div>
    <p>如果要将<code>0b</code>和<code>0o</code>前缀的字符串数值转为十进制，要使用<code>Number</code>方法。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Number</span>(<span class="pl-s"><span
            class="pl-pds">'</span>0b111<span class="pl-pds">'</span></span>)  <span class="pl-c"><span
            class="pl-c">//</span> 7</span>
<span class="pl-c1">Number</span>(<span class="pl-s"><span class="pl-pds">'</span>0o10<span
                class="pl-pds">'</span></span>)  <span class="pl-c"><span class="pl-c">//</span> 8</span></pre>
    </div>
    <h2><a href="#numberisfinite-numberisnan"  class="anchor"
           id="user-content-numberisfinite-numberisnan">

    </a>Number.isFinite(), Number.isNaN()
    </h2>
    <p>ES6 在<code>Number</code>对象上，新提供了<code>Number.isFinite()</code>和<code>Number.isNaN()</code>两个方法。</p>
    <p><code>Number.isFinite()</code>用来检查一个数值是否为有限的（finite）。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Number</span>.<span
            class="pl-en">isFinite</span>(<span class="pl-c1">15</span>); <span class="pl-c"><span
            class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isFinite</span>(<span class="pl-c1">0.8</span>); <span
                class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isFinite</span>(<span class="pl-c1">NaN</span>); <span
                class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isFinite</span>(<span class="pl-c1">Infinity</span>); <span
                class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isFinite</span>(<span class="pl-k">-</span><span class="pl-c1">Infinity</span>); <span
                class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isFinite</span>(<span class="pl-s"><span class="pl-pds">'</span>foo<span
                class="pl-pds">'</span></span>); <span class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isFinite</span>(<span class="pl-s"><span class="pl-pds">'</span>15<span
                class="pl-pds">'</span></span>); <span class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isFinite</span>(<span class="pl-c1">true</span>); <span
                class="pl-c"><span class="pl-c">//</span> false</span></pre>
    </div>
    <p>ES5 可以通过下面的代码，部署<code>Number.isFinite</code>方法。</p>
    <div class="highlight highlight-source-js"><pre>(<span class="pl-k">function</span> (<span
            class="pl-c1">global</span>) {
  <span class="pl-k">var</span> global_isFinite <span class="pl-k">=</span> <span class="pl-c1">global</span>.<span
                class="pl-smi">isFinite</span>;

  <span class="pl-c1">Object</span>.<span class="pl-en">defineProperty</span>(<span class="pl-c1">Number</span>, <span
                class="pl-s"><span class="pl-pds">'</span>isFinite<span class="pl-pds">'</span></span>, {
    <span class="pl-en">value</span><span class="pl-k">:</span> <span class="pl-k">function</span> <span class="pl-en">isFinite</span>(<span
                class="pl-smi">value</span>) {
      <span class="pl-k">return</span> <span class="pl-k">typeof</span> value <span class="pl-k">===</span> <span
                class="pl-s"><span class="pl-pds">'</span>number<span class="pl-pds">'</span></span> <span class="pl-k">&amp;&amp;</span> <span
                class="pl-en">global_isFinite</span>(value);
    },
    configurable<span class="pl-k">:</span> <span class="pl-c1">true</span>,
    enumerable<span class="pl-k">:</span> <span class="pl-c1">false</span>,
    writable<span class="pl-k">:</span> <span class="pl-c1">true</span>
  });
})(<span class="pl-c1">this</span>);</pre>
    </div>
    <p><code>Number.isNaN()</code>用来检查一个值是否为<code>NaN</code>。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Number</span>.<span
            class="pl-en">isNaN</span>(<span class="pl-c1">NaN</span>) <span class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isNaN</span>(<span class="pl-c1">15</span>) <span
                class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isNaN</span>(<span class="pl-s"><span
                class="pl-pds">'</span>15<span class="pl-pds">'</span></span>) <span class="pl-c"><span
                class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isNaN</span>(<span class="pl-c1">true</span>) <span
                class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isNaN</span>(<span class="pl-c1">9</span><span
                class="pl-k">/</span><span class="pl-c1">NaN</span>) <span class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isNaN</span>(<span class="pl-s"><span
                class="pl-pds">'</span>true<span class="pl-pds">'</span></span><span class="pl-k">/</span><span
                class="pl-c1">0</span>) <span class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isNaN</span>(<span class="pl-s"><span
                class="pl-pds">'</span>true<span class="pl-pds">'</span></span><span class="pl-k">/</span><span
                class="pl-s"><span class="pl-pds">'</span>true<span class="pl-pds">'</span></span>) <span
                class="pl-c"><span class="pl-c">//</span> true</span></pre>
    </div>
    <p>ES5 通过下面的代码，部署<code>Number.isNaN()</code>。</p>
    <div class="highlight highlight-source-js"><pre>(<span class="pl-k">function</span> (<span
            class="pl-c1">global</span>) {
  <span class="pl-k">var</span> global_isNaN <span class="pl-k">=</span> <span class="pl-c1">global</span>.<span
                class="pl-smi">isNaN</span>;

  <span class="pl-c1">Object</span>.<span class="pl-en">defineProperty</span>(<span class="pl-c1">Number</span>, <span
                class="pl-s"><span class="pl-pds">'</span>isNaN<span class="pl-pds">'</span></span>, {
    <span class="pl-en">value</span><span class="pl-k">:</span> <span class="pl-k">function</span> <span class="pl-en">isNaN</span>(<span
                class="pl-smi">value</span>) {
      <span class="pl-k">return</span> <span class="pl-k">typeof</span> value <span class="pl-k">===</span> <span
                class="pl-s"><span class="pl-pds">'</span>number<span class="pl-pds">'</span></span> <span class="pl-k">&amp;&amp;</span> <span
                class="pl-en">global_isNaN</span>(value);
    },
    configurable<span class="pl-k">:</span> <span class="pl-c1">true</span>,
    enumerable<span class="pl-k">:</span> <span class="pl-c1">false</span>,
    writable<span class="pl-k">:</span> <span class="pl-c1">true</span>
  });
})(<span class="pl-c1">this</span>);</pre>
    </div>
    <p>它们与传统的全局方法<code>isFinite()</code>和<code>isNaN()</code>的区别在于，传统方法先调用<code>Number()</code>将非数值的值转为数值，再进行判断，而这两个新方法只对数值有效，<code>Number.isFinite()</code>对于非数值一律返回<code>false</code>,
        <code>Number.isNaN()</code>只有对于<code>NaN</code>才返回<code>true</code>，非<code>NaN</code>一律返回<code>false</code>。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">isFinite</span>(<span
            class="pl-c1">25</span>) <span class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">isFinite</span>(<span class="pl-s"><span class="pl-pds">"</span>25<span
                class="pl-pds">"</span></span>) <span class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isFinite</span>(<span class="pl-c1">25</span>) <span class="pl-c"><span
                class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isFinite</span>(<span class="pl-s"><span class="pl-pds">"</span>25<span
                class="pl-pds">"</span></span>) <span class="pl-c"><span class="pl-c">//</span> false</span>

<span class="pl-c1">isNaN</span>(<span class="pl-c1">NaN</span>) <span class="pl-c"><span
                class="pl-c">//</span> true</span>
<span class="pl-c1">isNaN</span>(<span class="pl-s"><span class="pl-pds">"</span>NaN<span class="pl-pds">"</span></span>) <span
                class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isNaN</span>(<span class="pl-c1">NaN</span>) <span
                class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isNaN</span>(<span class="pl-s"><span
                class="pl-pds">"</span>NaN<span class="pl-pds">"</span></span>) <span class="pl-c"><span
                class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isNaN</span>(<span class="pl-c1">1</span>) <span
                class="pl-c"><span class="pl-c">//</span> false</span></pre>
    </div>
    <h2><a href="#numberparseint-numberparsefloat"  class="anchor"
           id="user-content-numberparseint-numberparsefloat">

    </a>Number.parseInt(), Number.parseFloat()
    </h2>
    <p>ES6 将全局方法<code>parseInt()</code>和<code>parseFloat()</code>，移植到<code>Number</code>对象上面，行为完全保持不变。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c"><span class="pl-c">//</span> ES5的写法</span>
<span class="pl-c1">parseInt</span>(<span class="pl-s"><span class="pl-pds">'</span>12.34<span
                class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> 12</span>
<span class="pl-c1">parseFloat</span>(<span class="pl-s"><span class="pl-pds">'</span>123.45#<span
                class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> 123.45</span>

<span class="pl-c"><span class="pl-c">//</span> ES6的写法</span>
<span class="pl-c1">Number</span>.<span class="pl-en">parseInt</span>(<span class="pl-s"><span class="pl-pds">'</span>12.34<span
                class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> 12</span>
<span class="pl-c1">Number</span>.<span class="pl-en">parseFloat</span>(<span class="pl-s"><span class="pl-pds">'</span>123.45#<span
                class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> 123.45</span></pre>
    </div>
    <p>这样做的目的，是逐步减少全局性方法，使得语言逐步模块化。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Number</span>.<span
            class="pl-smi">parseInt</span> <span class="pl-k">===</span> parseInt <span class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-smi">parseFloat</span> <span class="pl-k">===</span> parseFloat <span
                class="pl-c"><span class="pl-c">//</span> true</span></pre>
    </div>
    <h2><a href="#numberisinteger"  class="anchor" id="user-content-numberisinteger">

    </a>Number.isInteger()
    </h2>
    <p><code>Number.isInteger()</code>用来判断一个值是否为整数。需要注意的是，在 JavaScript 内部，整数和浮点数是同样的储存方法，所以 3 和 3.0 被视为同一个值。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Number</span>.<span
            class="pl-en">isInteger</span>(<span class="pl-c1">25</span>) <span class="pl-c"><span
            class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isInteger</span>(<span class="pl-c1">25.0</span>) <span
                class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isInteger</span>(<span class="pl-c1">25.1</span>) <span
                class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isInteger</span>(<span class="pl-s"><span class="pl-pds">"</span>15<span
                class="pl-pds">"</span></span>) <span class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isInteger</span>(<span class="pl-c1">true</span>) <span
                class="pl-c"><span class="pl-c">//</span> false</span></pre>
    </div>
    <p>ES5 可以通过下面的代码，部署<code>Number.isInteger()</code>。</p>
    <div class="highlight highlight-source-js"><pre>(<span class="pl-k">function</span> (<span
            class="pl-c1">global</span>) {
  <span class="pl-k">var</span> floor <span class="pl-k">=</span> <span class="pl-c1">Math</span>.<span class="pl-smi">floor</span>,
    isFinite <span class="pl-k">=</span> <span class="pl-c1">global</span>.<span class="pl-smi">isFinite</span>;

  <span class="pl-c1">Object</span>.<span class="pl-en">defineProperty</span>(<span class="pl-c1">Number</span>, <span
                class="pl-s"><span class="pl-pds">'</span>isInteger<span class="pl-pds">'</span></span>, {
    <span class="pl-en">value</span><span class="pl-k">:</span> <span class="pl-k">function</span> <span class="pl-en">isInteger</span>(<span
                class="pl-smi">value</span>) {
      <span class="pl-k">return</span> <span class="pl-k">typeof</span> value <span class="pl-k">===</span> <span
                class="pl-s"><span class="pl-pds">'</span>number<span class="pl-pds">'</span></span> <span class="pl-k">&amp;&amp;</span>
        <span class="pl-c1">isFinite</span>(value) <span class="pl-k">&amp;&amp;</span>
        <span class="pl-en">floor</span>(value) <span class="pl-k">===</span> value;
    },
    configurable<span class="pl-k">:</span> <span class="pl-c1">true</span>,
    enumerable<span class="pl-k">:</span> <span class="pl-c1">false</span>,
    writable<span class="pl-k">:</span> <span class="pl-c1">true</span>
  });
})(<span class="pl-c1">this</span>);</pre>
    </div>
    <h2><a href="#numberepsilon"  class="anchor" id="user-content-numberepsilon">

    </a>Number.EPSILON
    </h2>
    <p>ES6 在<code>Number</code>对象上面，新增一个极小的常量<code>Number.EPSILON</code>。根据规格，它表示 1 与大于 1 的最小浮点数之间的差。</p>
    <p>对于 64 位浮点数来说，大于 1 的最小浮点数相当于二进制的<code>1.00..001</code>，小数点后面有连续 51 个零。这个值减去 1 之后，就等于 2 的-52 次方。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Number</span>.<span class="pl-c1">EPSILON</span> <span
            class="pl-k">===</span> <span class="pl-c1">Math</span>.<span class="pl-c1">pow</span>(<span
            class="pl-c1">2</span>, <span class="pl-k">-</span><span class="pl-c1">52</span>)
<span class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-c1">EPSILON</span>
<span class="pl-c"><span class="pl-c">//</span> 2.220446049250313e-16</span>
<span class="pl-c1">Number</span>.<span class="pl-c1">EPSILON</span>.<span class="pl-en">toFixed</span>(<span
                class="pl-c1">20</span>)
<span class="pl-c"><span class="pl-c">//</span> "0.00000000000000022204"</span></pre>
    </div>
    <p><code>Number.EPSILON</code>实际上是 JavaScript 能够表示的最小精度。误差如果小于这个值，就可以认为已经没有意义了，即不存在误差了。</p>
    <p>引入一个这么小的量的目的，在于为浮点数计算，设置一个误差范围。我们知道浮点数计算是不精确的。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">0.1</span> <span class="pl-k">+</span> <span
            class="pl-c1">0.2</span>
<span class="pl-c"><span class="pl-c">//</span> 0.30000000000000004</span>

<span class="pl-c1">0.1</span> <span class="pl-k">+</span> <span class="pl-c1">0.2</span> <span
                class="pl-k">-</span> <span class="pl-c1">0.3</span>
<span class="pl-c"><span class="pl-c">//</span> 5.551115123125783e-17</span>

<span class="pl-c1">5.551115123125783e-17</span>.<span class="pl-en">toFixed</span>(<span class="pl-c1">20</span>)
<span class="pl-c"><span class="pl-c">//</span> '0.00000000000000005551'</span></pre>
    </div>
    <p>上面代码解释了，为什么比较<code>0.1 + 0.2</code>与<code>0.3</code>得到的结果是<code>false</code>。</p>
    <div class="highlight highlight-source-js">
        <pre><span class="pl-c1">0.1</span> <span class="pl-k">+</span> <span class="pl-c1">0.2</span> <span
                class="pl-k">===</span> <span class="pl-c1">0.3</span> <span class="pl-c"><span class="pl-c">//</span> false</span></pre>
    </div>
    <p><code>Number.EPSILON</code>可以用来设置“能够接受的误差范围”。比如，误差范围设为 2 的-50 次方（即<code>Number.EPSILON * Math.pow(2, 2)</code>），即如果两个浮点数的差小于这个值，我们就认为这两个浮点数相等。
    </p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">5.551115123125783e-17</span> <span class="pl-k">&lt;</span> <span
            class="pl-c1">Number</span>.<span class="pl-c1">EPSILON</span> <span class="pl-k">*</span> <span
            class="pl-c1">Math</span>.<span class="pl-c1">pow</span>(<span class="pl-c1">2</span>, <span
            class="pl-c1">2</span>)
<span class="pl-c"><span class="pl-c">//</span> true</span></pre>
    </div>
    <p>因此，<code>Number.EPSILON</code>的实质是一个可以接受的最小误差范围。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">function</span> <span class="pl-en">withinErrorMargin</span> (<span
            class="pl-smi">left</span>, <span class="pl-smi">right</span>) {
  <span class="pl-k">return</span> <span class="pl-c1">Math</span>.<span class="pl-c1">abs</span>(left <span
                class="pl-k">-</span> right) <span class="pl-k">&lt;</span> <span class="pl-c1">Number</span>.<span
                class="pl-c1">EPSILON</span> <span class="pl-k">*</span> <span class="pl-c1">Math</span>.<span
                class="pl-c1">pow</span>(<span class="pl-c1">2</span>, <span class="pl-c1">2</span>);
}

<span class="pl-c1">0.1</span> <span class="pl-k">+</span> <span class="pl-c1">0.2</span> <span class="pl-k">===</span> <span
                class="pl-c1">0.3</span> <span class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-en">withinErrorMargin</span>(<span class="pl-c1">0.1</span> <span class="pl-k">+</span> <span
                class="pl-c1">0.2</span>, <span class="pl-c1">0.3</span>) <span class="pl-c"><span
                class="pl-c">//</span> true</span>

<span class="pl-c1">1.1</span> <span class="pl-k">+</span> <span class="pl-c1">1.3</span> <span class="pl-k">===</span> <span
                class="pl-c1">2.4</span> <span class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-en">withinErrorMargin</span>(<span class="pl-c1">1.1</span> <span class="pl-k">+</span> <span
                class="pl-c1">1.3</span>, <span class="pl-c1">2.4</span>) <span class="pl-c"><span
                class="pl-c">//</span> true</span></pre>
    </div>
    <p>上面的代码为浮点数运算，部署了一个误差检查函数。</p>
    <h2><a href="#安全整数和-numberissafeinteger"  class="anchor"
           id="user-content-安全整数和-numberissafeinteger">

    </a>安全整数和 Number.isSafeInteger()
    </h2>
    <p>JavaScript 能够准确表示的整数范围在<code>-2^53</code>到<code>2^53</code>之间（不含两个端点），超过这个范围，无法精确表示这个值。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span class="pl-c1">pow</span>(<span
            class="pl-c1">2</span>, <span class="pl-c1">53</span>) <span class="pl-c"><span class="pl-c">//</span> 9007199254740992</span>

<span class="pl-c1">9007199254740992</span>  <span class="pl-c"><span class="pl-c">//</span> 9007199254740992</span>
<span class="pl-c1">9007199254740993</span>  <span class="pl-c"><span class="pl-c">//</span> 9007199254740992</span>

<span class="pl-c1">Math</span>.<span class="pl-c1">pow</span>(<span class="pl-c1">2</span>, <span
                class="pl-c1">53</span>) <span class="pl-k">===</span> <span class="pl-c1">Math</span>.<span
                class="pl-c1">pow</span>(<span class="pl-c1">2</span>, <span class="pl-c1">53</span>) <span
                class="pl-k">+</span> <span class="pl-c1">1</span>
<span class="pl-c"><span class="pl-c">//</span> true</span></pre>
    </div>
    <p>上面代码中，超出 2 的 53 次方之后，一个数就不精确了。</p>
    <p>ES6 引入了<code>Number.MAX_SAFE_INTEGER</code>和<code>Number.MIN_SAFE_INTEGER</code>这两个常量，用来表示这个范围的上下限。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Number</span>.<span class="pl-c1">MAX_SAFE_INTEGER</span> <span
            class="pl-k">===</span> <span class="pl-c1">Math</span>.<span class="pl-c1">pow</span>(<span
            class="pl-c1">2</span>, <span class="pl-c1">53</span>) <span class="pl-k">-</span> <span
            class="pl-c1">1</span>
<span class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-c1">MAX_SAFE_INTEGER</span> <span class="pl-k">===</span> <span
                class="pl-c1">9007199254740991</span>
<span class="pl-c"><span class="pl-c">//</span> true</span>

<span class="pl-c1">Number</span>.<span class="pl-c1">MIN_SAFE_INTEGER</span> <span class="pl-k">===</span> <span
                class="pl-k">-</span><span class="pl-c1">Number</span>.<span class="pl-c1">MAX_SAFE_INTEGER</span>
<span class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-c1">MIN_SAFE_INTEGER</span> <span class="pl-k">===</span> <span
                class="pl-k">-</span><span class="pl-c1">9007199254740991</span>
<span class="pl-c"><span class="pl-c">//</span> true</span></pre>
    </div>
    <p>上面代码中，可以看到 JavaScript 能够精确表示的极限。</p>
    <p><code>Number.isSafeInteger()</code>则是用来判断一个整数是否落在这个范围之内。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Number</span>.<span
            class="pl-en">isSafeInteger</span>(<span class="pl-s"><span class="pl-pds">'</span>a<span
            class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isSafeInteger</span>(<span class="pl-c1">null</span>) <span
                class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isSafeInteger</span>(<span class="pl-c1">NaN</span>) <span
                class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isSafeInteger</span>(<span class="pl-c1">Infinity</span>) <span
                class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isSafeInteger</span>(<span class="pl-k">-</span><span
                class="pl-c1">Infinity</span>) <span class="pl-c"><span class="pl-c">//</span> false</span>

<span class="pl-c1">Number</span>.<span class="pl-en">isSafeInteger</span>(<span class="pl-c1">3</span>) <span
                class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isSafeInteger</span>(<span class="pl-c1">1.2</span>) <span
                class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isSafeInteger</span>(<span class="pl-c1">9007199254740990</span>) <span
                class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isSafeInteger</span>(<span class="pl-c1">9007199254740992</span>) <span
                class="pl-c"><span class="pl-c">//</span> false</span>

<span class="pl-c1">Number</span>.<span class="pl-en">isSafeInteger</span>(<span class="pl-c1">Number</span>.<span
                class="pl-c1">MIN_SAFE_INTEGER</span> <span class="pl-k">-</span> <span class="pl-c1">1</span>) <span
                class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isSafeInteger</span>(<span class="pl-c1">Number</span>.<span
                class="pl-c1">MIN_SAFE_INTEGER</span>) <span class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isSafeInteger</span>(<span class="pl-c1">Number</span>.<span
                class="pl-c1">MAX_SAFE_INTEGER</span>) <span class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isSafeInteger</span>(<span class="pl-c1">Number</span>.<span
                class="pl-c1">MAX_SAFE_INTEGER</span> <span class="pl-k">+</span> <span class="pl-c1">1</span>) <span
                class="pl-c"><span class="pl-c">//</span> false</span></pre>
    </div>
    <p>这个函数的实现很简单，就是跟安全整数的两个边界值比较一下。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Number</span>.<span
            class="pl-en">isSafeInteger</span> <span class="pl-k">=</span> <span class="pl-k">function</span> (<span
            class="pl-smi">n</span>) {
  <span class="pl-k">return</span> (<span class="pl-k">typeof</span> n <span class="pl-k">===</span> <span class="pl-s"><span
                class="pl-pds">'</span>number<span class="pl-pds">'</span></span> <span class="pl-k">&amp;&amp;</span>
    <span class="pl-c1">Math</span>.<span class="pl-c1">round</span>(n) <span class="pl-k">===</span> n <span
                class="pl-k">&amp;&amp;</span>
    <span class="pl-c1">Number</span>.<span class="pl-c1">MIN_SAFE_INTEGER</span> <span
                class="pl-k">&lt;=</span> n <span class="pl-k">&amp;&amp;</span>
    n <span class="pl-k">&lt;=</span> <span class="pl-c1">Number</span>.<span class="pl-c1">MAX_SAFE_INTEGER</span>);
}</pre>
    </div>
    <p>实际使用这个函数时，需要注意。验证运算结果是否落在安全整数的范围内，不要只验证运算结果，而要同时验证参与运算的每个值。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Number</span>.<span
            class="pl-en">isSafeInteger</span>(<span class="pl-c1">9007199254740993</span>)
<span class="pl-c"><span class="pl-c">//</span> false</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isSafeInteger</span>(<span class="pl-c1">990</span>)
<span class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">Number</span>.<span class="pl-en">isSafeInteger</span>(<span
                class="pl-c1">9007199254740993</span> <span class="pl-k">-</span> <span class="pl-c1">990</span>)
<span class="pl-c"><span class="pl-c">//</span> true</span>
<span class="pl-c1">9007199254740993</span> <span class="pl-k">-</span> <span class="pl-c1">990</span>
<span class="pl-c"><span class="pl-c">//</span> 返回结果 9007199254740002</span>
<span class="pl-c"><span class="pl-c">//</span> 正确答案应该是 9007199254740003</span></pre>
    </div>
    <p>上面代码中，<code>9007199254740993</code>不是一个安全整数，但是<code>Number.isSafeInteger</code>会返回结果，显示计算结果是安全的。这是因为，这个数超出了精度范围，导致在计算机内部，以<code>9007199254740992</code>的形式储存。
    </p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">9007199254740993</span> <span
            class="pl-k">===</span> <span class="pl-c1">9007199254740992</span>
<span class="pl-c"><span class="pl-c">//</span> true</span></pre>
    </div>
    <p>所以，如果只验证运算结果是否为安全整数，很可能得到错误结果。下面的函数可以同时验证两个运算数和运算结果。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">function</span> <span class="pl-en">trusty</span> (<span
            class="pl-smi">left</span>, <span class="pl-smi">right</span>, <span class="pl-smi">result</span>) {
  <span class="pl-k">if</span> (
    <span class="pl-c1">Number</span>.<span class="pl-en">isSafeInteger</span>(left) <span
                class="pl-k">&amp;&amp;</span>
    <span class="pl-c1">Number</span>.<span class="pl-en">isSafeInteger</span>(right) <span
                class="pl-k">&amp;&amp;</span>
    <span class="pl-c1">Number</span>.<span class="pl-en">isSafeInteger</span>(result)
  ) {
    <span class="pl-k">return</span> result;
  }
  <span class="pl-k">throw</span> <span class="pl-k">new</span> <span class="pl-en">RangeError</span>(<span
                class="pl-s"><span class="pl-pds">'</span>Operation cannot be trusted!<span
                class="pl-pds">'</span></span>);
}

<span class="pl-en">trusty</span>(<span class="pl-c1">9007199254740993</span>, <span class="pl-c1">990</span>, <span
                class="pl-c1">9007199254740993</span> <span class="pl-k">-</span> <span class="pl-c1">990</span>)
<span class="pl-c"><span class="pl-c">//</span> RangeError: Operation cannot be trusted!</span>

<span class="pl-en">trusty</span>(<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span
                class="pl-c1">3</span>)
<span class="pl-c"><span class="pl-c">//</span> 3</span></pre>
    </div>
    <h2><a href="#math-对象的扩展"  class="anchor" id="user-content-math-对象的扩展">

    </a>Math 对象的扩展
    </h2>
    <p>ES6 在 Math 对象上新增了 17 个与数学相关的方法。所有这些方法都是静态方法，只能在 Math 对象上调用。</p>
    <h3><a href="#mathtrunc"  class="anchor" id="user-content-mathtrunc">

    </a>Math.trunc()
    </h3>
    <p><code>Math.trunc</code>方法用于去除一个数的小数部分，返回整数部分。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">trunc</span>(<span class="pl-c1">4.1</span>) <span class="pl-c"><span class="pl-c">//</span> 4</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">trunc</span>(<span class="pl-c1">4.9</span>) <span
                class="pl-c"><span class="pl-c">//</span> 4</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">trunc</span>(<span class="pl-k">-</span><span
                class="pl-c1">4.1</span>) <span class="pl-c"><span class="pl-c">//</span> -4</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">trunc</span>(<span class="pl-k">-</span><span
                class="pl-c1">4.9</span>) <span class="pl-c"><span class="pl-c">//</span> -4</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">trunc</span>(<span class="pl-k">-</span><span
                class="pl-c1">0.1234</span>) <span class="pl-c"><span class="pl-c">//</span> -0</span></pre>
    </div>
    <p>对于非数值，<code>Math.trunc</code>内部使用<code>Number</code>方法将其先转为数值。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">trunc</span>(<span class="pl-s"><span class="pl-pds">'</span>123.456<span
            class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> 123</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">trunc</span>(<span class="pl-c1">true</span>) <span
                class="pl-c"><span class="pl-c">//</span>1</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">trunc</span>(<span class="pl-c1">false</span>) <span
                class="pl-c"><span class="pl-c">//</span> 0</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">trunc</span>(<span class="pl-c1">null</span>) <span
                class="pl-c"><span class="pl-c">//</span> 0</span></pre>
    </div>
    <p>对于空值和无法截取整数的值，返回<code>NaN</code>。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">trunc</span>(<span class="pl-c1">NaN</span>);      <span class="pl-c"><span
            class="pl-c">//</span> NaN</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">trunc</span>(<span class="pl-s"><span
                class="pl-pds">'</span>foo<span class="pl-pds">'</span></span>);    <span class="pl-c"><span
                class="pl-c">//</span> NaN</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">trunc</span>();         <span class="pl-c"><span
                class="pl-c">//</span> NaN</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">trunc</span>(<span class="pl-c1">undefined</span>) <span
                class="pl-c"><span class="pl-c">//</span> NaN</span></pre>
    </div>
    <p>对于没有部署这个方法的环境，可以用下面的代码模拟。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-smi">trunc</span> <span class="pl-k">=</span> <span class="pl-c1">Math</span>.<span
            class="pl-smi">trunc</span> <span class="pl-k">||</span> <span class="pl-k">function</span>(<span
            class="pl-smi">x</span>) {
  <span class="pl-k">return</span> x <span class="pl-k">&lt;</span> <span class="pl-c1">0</span> <span
                class="pl-k">?</span> <span class="pl-c1">Math</span>.<span class="pl-c1">ceil</span>(x) <span
                class="pl-k">:</span> <span class="pl-c1">Math</span>.<span class="pl-c1">floor</span>(x);
};</pre>
    </div>
    <h3><a href="#mathsign"  class="anchor" id="user-content-mathsign">

    </a>Math.sign()
    </h3>
    <p><code>Math.sign</code>方法用来判断一个数到底是正数、负数、还是零。对于非数值，会先将其转换为数值。</p>
    <p>它会返回五种值。</p>
    <ul>
        <li>参数为正数，返回<code>+1</code>；</li>
        <li>参数为负数，返回<code>-1</code>；</li>
        <li>参数为 0，返回<code>0</code>；</li>
        <li>参数为-0，返回<code>-0</code>;</li>
        <li>其他值，返回<code>NaN</code>。</li>
    </ul>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">sign</span>(<span class="pl-k">-</span><span class="pl-c1">5</span>) <span class="pl-c"><span
            class="pl-c">//</span> -1</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">sign</span>(<span class="pl-c1">5</span>) <span class="pl-c"><span
                class="pl-c">//</span> +1</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">sign</span>(<span class="pl-c1">0</span>) <span class="pl-c"><span
                class="pl-c">//</span> +0</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">sign</span>(<span class="pl-k">-</span><span class="pl-c1">0</span>) <span
                class="pl-c"><span class="pl-c">//</span> -0</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">sign</span>(<span class="pl-c1">NaN</span>) <span class="pl-c"><span
                class="pl-c">//</span> NaN</span></pre>
    </div>
    <p>如果参数是非数值，会自动转为数值。对于那些无法转为数值的值，会返回<code>NaN</code>。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">sign</span>(<span class="pl-s"><span class="pl-pds">'</span><span
            class="pl-pds">'</span></span>)  <span class="pl-c"><span class="pl-c">//</span> 0</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">sign</span>(<span class="pl-c1">true</span>)  <span
                class="pl-c"><span class="pl-c">//</span> +1</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">sign</span>(<span class="pl-c1">false</span>)  <span
                class="pl-c"><span class="pl-c">//</span> 0</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">sign</span>(<span class="pl-c1">null</span>)  <span
                class="pl-c"><span class="pl-c">//</span> 0</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">sign</span>(<span class="pl-s"><span class="pl-pds">'</span>9<span
                class="pl-pds">'</span></span>)  <span class="pl-c"><span class="pl-c">//</span> +1</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">sign</span>(<span class="pl-s"><span class="pl-pds">'</span>foo<span
                class="pl-pds">'</span></span>)  <span class="pl-c"><span class="pl-c">//</span> NaN</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">sign</span>()  <span class="pl-c"><span
                class="pl-c">//</span> NaN</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">sign</span>(<span class="pl-c1">undefined</span>)  <span
                class="pl-c"><span class="pl-c">//</span> NaN</span></pre>
    </div>
    <p>对于没有部署这个方法的环境，可以用下面的代码模拟。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-smi">sign</span> <span class="pl-k">=</span> <span class="pl-c1">Math</span>.<span class="pl-smi">sign</span> <span
            class="pl-k">||</span> <span class="pl-k">function</span>(<span class="pl-smi">x</span>) {
  x <span class="pl-k">=</span> <span class="pl-k">+</span>x; <span class="pl-c"><span class="pl-c">//</span> convert to a number</span>
  <span class="pl-k">if</span> (x <span class="pl-k">===</span> <span class="pl-c1">0</span> <span
                class="pl-k">||</span> <span class="pl-c1">isNaN</span>(x)) {
    <span class="pl-k">return</span> x;
  }
  <span class="pl-k">return</span> x <span class="pl-k">&gt;</span> <span class="pl-c1">0</span> <span
                class="pl-k">?</span> <span class="pl-c1">1</span> <span class="pl-k">:</span> <span
                class="pl-k">-</span><span class="pl-c1">1</span>;
};</pre>
    </div>
    <h3><a href="#mathcbrt"  class="anchor" id="user-content-mathcbrt">

    </a>Math.cbrt()
    </h3>
    <p><code>Math.cbrt</code>方法用于计算一个数的立方根。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">cbrt</span>(<span class="pl-k">-</span><span class="pl-c1">1</span>) <span class="pl-c"><span
            class="pl-c">//</span> -1</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">cbrt</span>(<span class="pl-c1">0</span>)  <span class="pl-c"><span
                class="pl-c">//</span> 0</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">cbrt</span>(<span class="pl-c1">1</span>)  <span class="pl-c"><span
                class="pl-c">//</span> 1</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">cbrt</span>(<span class="pl-c1">2</span>)  <span class="pl-c"><span
                class="pl-c">//</span> 1.2599210498948734</span></pre>
    </div>
    <p>对于非数值，<code>Math.cbrt</code>方法内部也是先使用<code>Number</code>方法将其转为数值。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">cbrt</span>(<span class="pl-s"><span class="pl-pds">'</span>8<span
            class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> 2</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">cbrt</span>(<span class="pl-s"><span
                class="pl-pds">'</span>hello<span class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> NaN</span></pre>
    </div>
    <p>对于没有部署这个方法的环境，可以用下面的代码模拟。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-smi">cbrt</span> <span class="pl-k">=</span> <span class="pl-c1">Math</span>.<span class="pl-smi">cbrt</span> <span
            class="pl-k">||</span> <span class="pl-k">function</span>(<span class="pl-smi">x</span>) {
  <span class="pl-k">var</span> y <span class="pl-k">=</span> <span class="pl-c1">Math</span>.<span
                class="pl-c1">pow</span>(<span class="pl-c1">Math</span>.<span class="pl-c1">abs</span>(x), <span
                class="pl-c1">1</span><span class="pl-k">/</span><span class="pl-c1">3</span>);
  <span class="pl-k">return</span> x <span class="pl-k">&lt;</span> <span class="pl-c1">0</span> <span
                class="pl-k">?</span> <span class="pl-k">-</span>y <span class="pl-k">:</span> y;
};</pre>
    </div>
    <h3><a href="#mathclz32"  class="anchor" id="user-content-mathclz32">

    </a>Math.clz32()
    </h3>
    <p>JavaScript 的整数使用 32 位二进制形式表示，<code>Math.clz32</code>方法返回一个数的 32 位无符号整数形式有多少个前导 0。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">clz32</span>(<span class="pl-c1">0</span>) <span class="pl-c"><span class="pl-c">//</span> 32</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">clz32</span>(<span class="pl-c1">1</span>) <span class="pl-c"><span
                class="pl-c">//</span> 31</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">clz32</span>(<span class="pl-c1">1000</span>) <span
                class="pl-c"><span class="pl-c">//</span> 22</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">clz32</span>(<span
                class="pl-c1">0b01000000000000000000000000000000</span>) <span class="pl-c"><span class="pl-c">//</span> 1</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">clz32</span>(<span
                class="pl-c1">0b00100000000000000000000000000000</span>) <span class="pl-c"><span class="pl-c">//</span> 2</span></pre>
    </div>
    <p>上面代码中，0 的二进制形式全为 0，所以有 32 个前导 0；1 的二进制形式是<code>0b1</code>，只占 1 位，所以 32 位之中有 31 个前导 0；1000 的二进制形式是<code>0b1111101000</code>，一共有
        10 位，所以 32 位之中有 22 个前导 0。</p>
    <p><code>clz32</code>这个函数名就来自”count leading zero bits in 32-bit binary representation of a number“（计算一个数的 32
        位二进制形式的前导 0 的个数）的缩写。</p>
    <p>左移运算符（<code>&lt;&lt;</code>）与<code>Math.clz32</code>方法直接相关。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">clz32</span>(<span class="pl-c1">0</span>) <span class="pl-c"><span class="pl-c">//</span> 32</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">clz32</span>(<span class="pl-c1">1</span>) <span class="pl-c"><span
                class="pl-c">//</span> 31</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">clz32</span>(<span class="pl-c1">1</span> <span class="pl-k">&lt;&lt;</span> <span
                class="pl-c1">1</span>) <span class="pl-c"><span class="pl-c">//</span> 30</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">clz32</span>(<span class="pl-c1">1</span> <span class="pl-k">&lt;&lt;</span> <span
                class="pl-c1">2</span>) <span class="pl-c"><span class="pl-c">//</span> 29</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">clz32</span>(<span class="pl-c1">1</span> <span class="pl-k">&lt;&lt;</span> <span
                class="pl-c1">29</span>) <span class="pl-c"><span class="pl-c">//</span> 2</span></pre>
    </div>
    <p>对于小数，<code>Math.clz32</code>方法只考虑整数部分。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">clz32</span>(<span class="pl-c1">3.2</span>) <span class="pl-c"><span class="pl-c">//</span> 30</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">clz32</span>(<span class="pl-c1">3.9</span>) <span
                class="pl-c"><span class="pl-c">//</span> 30</span></pre>
    </div>
    <p>对于空值或其他类型的值，<code>Math.clz32</code>方法会将它们先转为数值，然后再计算。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">clz32</span>() <span class="pl-c"><span class="pl-c">//</span> 32</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">clz32</span>(<span class="pl-c1">NaN</span>) <span
                class="pl-c"><span class="pl-c">//</span> 32</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">clz32</span>(<span class="pl-c1">Infinity</span>) <span
                class="pl-c"><span class="pl-c">//</span> 32</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">clz32</span>(<span class="pl-c1">null</span>) <span
                class="pl-c"><span class="pl-c">//</span> 32</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">clz32</span>(<span class="pl-s"><span
                class="pl-pds">'</span>foo<span class="pl-pds">'</span></span>) <span class="pl-c"><span
                class="pl-c">//</span> 32</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">clz32</span>([]) <span class="pl-c"><span class="pl-c">//</span> 32</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">clz32</span>({}) <span class="pl-c"><span class="pl-c">//</span> 32</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">clz32</span>(<span class="pl-c1">true</span>) <span
                class="pl-c"><span class="pl-c">//</span> 31</span></pre>
    </div>
    <h3><a href="#mathimul"  class="anchor" id="user-content-mathimul">

    </a>Math.imul()
    </h3>
    <p><code>Math.imul</code>方法返回两个数以 32 位带符号整数形式相乘的结果，返回的也是一个 32 位的带符号整数。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">imul</span>(<span class="pl-c1">2</span>, <span class="pl-c1">4</span>)   <span
            class="pl-c"><span class="pl-c">//</span> 8</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">imul</span>(<span class="pl-k">-</span><span class="pl-c1">1</span>, <span
                class="pl-c1">8</span>)  <span class="pl-c"><span class="pl-c">//</span> -8</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">imul</span>(<span class="pl-k">-</span><span class="pl-c1">2</span>, <span
                class="pl-k">-</span><span class="pl-c1">2</span>) <span class="pl-c"><span
                class="pl-c">//</span> 4</span></pre>
    </div>
    <p>如果只考虑最后 32 位，大多数情况下，<code>Math.imul(a, b)</code>与<code>a * b</code>的结果是相同的，即该方法等同于<code>(a * b)|0</code>的效果（超过 32
        位的部分溢出）。之所以需要部署这个方法，是因为 JavaScript 有精度限制，超过 2 的 53
        次方的值无法精确表示。这就是说，对于那些很大的数的乘法，低位数值往往都是不精确的，<code>Math.imul</code>方法可以返回正确的低位数值。</p>
    <div class="highlight highlight-source-js">
        <pre>(<span class="pl-c1">0x7fffffff</span> <span class="pl-k">*</span> <span
                class="pl-c1">0x7fffffff</span>)<span class="pl-k">|</span><span class="pl-c1">0</span> <span
                class="pl-c"><span class="pl-c">//</span> 0</span></pre>
    </div>
    <p>上面这个乘法算式，返回结果为 0。但是由于这两个二进制数的最低位都是 1，所以这个结果肯定是不正确的，因为根据二进制乘法，计算结果的二进制最低位应该也是 1。这个错误就是因为它们的乘积超过了 2 的 53
        次方，JavaScript 无法保存额外的精度，就把低位的值都变成了 0。<code>Math.imul</code>方法可以返回正确的值 1。</p>
    <div class="highlight highlight-source-js">
        <pre><span class="pl-c1">Math</span>.<span class="pl-c1">imul</span>(<span
                class="pl-c1">0x7fffffff</span>, <span class="pl-c1">0x7fffffff</span>) <span class="pl-c"><span
                class="pl-c">//</span> 1</span></pre>
    </div>
    <h3><a href="#mathfround"  class="anchor" id="user-content-mathfround">

    </a>Math.fround()
    </h3>
    <p>Math.fround 方法返回一个数的单精度浮点数形式。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">fround</span>(<span class="pl-c1">0</span>)     <span class="pl-c"><span
            class="pl-c">//</span> 0</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">fround</span>(<span class="pl-c1">1</span>)     <span
                class="pl-c"><span class="pl-c">//</span> 1</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">fround</span>(<span class="pl-c1">1.337</span>) <span
                class="pl-c"><span class="pl-c">//</span> 1.3370000123977661</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">fround</span>(<span class="pl-c1">1.5</span>)   <span
                class="pl-c"><span class="pl-c">//</span> 1.5</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">fround</span>(<span class="pl-c1">NaN</span>)   <span
                class="pl-c"><span class="pl-c">//</span> NaN</span></pre>
    </div>
    <p>对于整数来说，<code>Math.fround</code>方法返回结果不会有任何不同，区别主要是那些无法用 64 个二进制位精确表示的小数。这时，<code>Math.fround</code>方法会返回最接近这个小数的单精度浮点数。
    </p>
    <p>对于没有部署这个方法的环境，可以用下面的代码模拟。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-smi">fround</span> <span class="pl-k">=</span> <span class="pl-c1">Math</span>.<span
            class="pl-smi">fround</span> <span class="pl-k">||</span> <span class="pl-k">function</span>(<span
            class="pl-smi">x</span>) {
  <span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-en">Float32Array</span>([x])[<span
                class="pl-c1">0</span>];
};</pre>
    </div>
    <h3><a href="#mathhypot"  class="anchor" id="user-content-mathhypot">

    </a>Math.hypot()
    </h3>
    <p><code>Math.hypot</code>方法返回所有参数的平方和的平方根。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">hypot</span>(<span class="pl-c1">3</span>, <span class="pl-c1">4</span>);        <span
            class="pl-c"><span class="pl-c">//</span> 5</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">hypot</span>(<span class="pl-c1">3</span>, <span
                class="pl-c1">4</span>, <span class="pl-c1">5</span>);     <span class="pl-c"><span
                class="pl-c">//</span> 7.0710678118654755</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">hypot</span>();            <span class="pl-c"><span
                class="pl-c">//</span> 0</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">hypot</span>(<span class="pl-c1">NaN</span>);         <span
                class="pl-c"><span class="pl-c">//</span> NaN</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">hypot</span>(<span class="pl-c1">3</span>, <span
                class="pl-c1">4</span>, <span class="pl-s"><span class="pl-pds">'</span>foo<span class="pl-pds">'</span></span>); <span
                class="pl-c"><span class="pl-c">//</span> NaN</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">hypot</span>(<span class="pl-c1">3</span>, <span
                class="pl-c1">4</span>, <span class="pl-s"><span class="pl-pds">'</span>5<span
                class="pl-pds">'</span></span>);   <span class="pl-c"><span
                class="pl-c">//</span> 7.0710678118654755</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">hypot</span>(<span class="pl-k">-</span><span class="pl-c1">3</span>);          <span
                class="pl-c"><span class="pl-c">//</span> 3</span></pre>
    </div>
    <p>上面代码中，3 的平方加上 4 的平方，等于 5 的平方。</p>
    <p>如果参数不是数值，<code>Math.hypot</code>方法会将其转为数值。只要有一个参数无法转为数值，就会返回 NaN。</p>
    <h3><a href="#对数方法"  class="anchor" id="user-content-对数方法">

    </a>对数方法
    </h3>
    <p>ES6 新增了 4 个对数相关方法。</p>
    <p><strong>（1） Math.expm1()</strong></p>
    <p><code>Math.expm1(x)</code>返回 e<sup>x</sup> - 1，即<code>Math.exp(x) - 1</code>。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">expm1</span>(<span class="pl-k">-</span><span class="pl-c1">1</span>) <span class="pl-c"><span
            class="pl-c">//</span> -0.6321205588285577</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">expm1</span>(<span class="pl-c1">0</span>)  <span class="pl-c"><span
                class="pl-c">//</span> 0</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">expm1</span>(<span class="pl-c1">1</span>)  <span class="pl-c"><span
                class="pl-c">//</span> 1.718281828459045</span></pre>
    </div>
    <p>对于没有部署这个方法的环境，可以用下面的代码模拟。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-smi">expm1</span> <span class="pl-k">=</span> <span class="pl-c1">Math</span>.<span
            class="pl-smi">expm1</span> <span class="pl-k">||</span> <span class="pl-k">function</span>(<span
            class="pl-smi">x</span>) {
  <span class="pl-k">return</span> <span class="pl-c1">Math</span>.<span class="pl-c1">exp</span>(x) <span class="pl-k">-</span> <span
                class="pl-c1">1</span>;
};</pre>
    </div>
    <p><strong>（2）Math.log1p()</strong></p>
    <p><code>Math.log1p(x)</code>方法返回<code>1 + x</code>的自然对数，即<code>Math.log(1 + x)</code>。如果<code>x</code>小于-1，返回<code>NaN</code>。
    </p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">log1p</span>(<span class="pl-c1">1</span>)  <span class="pl-c"><span class="pl-c">//</span> 0.6931471805599453</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">log1p</span>(<span class="pl-c1">0</span>)  <span class="pl-c"><span
                class="pl-c">//</span> 0</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">log1p</span>(<span class="pl-k">-</span><span class="pl-c1">1</span>) <span
                class="pl-c"><span class="pl-c">//</span> -Infinity</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">log1p</span>(<span class="pl-k">-</span><span class="pl-c1">2</span>) <span
                class="pl-c"><span class="pl-c">//</span> NaN</span></pre>
    </div>
    <p>对于没有部署这个方法的环境，可以用下面的代码模拟。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-smi">log1p</span> <span class="pl-k">=</span> <span class="pl-c1">Math</span>.<span
            class="pl-smi">log1p</span> <span class="pl-k">||</span> <span class="pl-k">function</span>(<span
            class="pl-smi">x</span>) {
  <span class="pl-k">return</span> <span class="pl-c1">Math</span>.<span class="pl-c1">log</span>(<span
                class="pl-c1">1</span> <span class="pl-k">+</span> x);
};</pre>
    </div>
    <p><strong>（3）Math.log10()</strong></p>
    <p><code>Math.log10(x)</code>返回以 10 为底的<code>x</code>的对数。如果<code>x</code>小于 0，则返回 NaN。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">log10</span>(<span class="pl-c1">2</span>)      <span class="pl-c"><span
            class="pl-c">//</span> 0.3010299956639812</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">log10</span>(<span class="pl-c1">1</span>)      <span
                class="pl-c"><span class="pl-c">//</span> 0</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">log10</span>(<span class="pl-c1">0</span>)      <span
                class="pl-c"><span class="pl-c">//</span> -Infinity</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">log10</span>(<span class="pl-k">-</span><span class="pl-c1">2</span>)     <span
                class="pl-c"><span class="pl-c">//</span> NaN</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">log10</span>(<span class="pl-c1">100000</span>) <span
                class="pl-c"><span class="pl-c">//</span> 5</span></pre>
    </div>
    <p>对于没有部署这个方法的环境，可以用下面的代码模拟。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-smi">log10</span> <span class="pl-k">=</span> <span class="pl-c1">Math</span>.<span
            class="pl-smi">log10</span> <span class="pl-k">||</span> <span class="pl-k">function</span>(<span
            class="pl-smi">x</span>) {
  <span class="pl-k">return</span> <span class="pl-c1">Math</span>.<span class="pl-c1">log</span>(x) <span class="pl-k">/</span> <span
                class="pl-c1">Math</span>.<span class="pl-c1">LN10</span>;
};</pre>
    </div>
    <p><strong>（4）Math.log2()</strong></p>
    <p><code>Math.log2(x)</code>返回以 2 为底的<code>x</code>的对数。如果<code>x</code>小于 0，则返回 NaN。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-c1">log2</span>(<span class="pl-c1">3</span>)       <span class="pl-c"><span
            class="pl-c">//</span> 1.584962500721156</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">log2</span>(<span class="pl-c1">2</span>)       <span
                class="pl-c"><span class="pl-c">//</span> 1</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">log2</span>(<span class="pl-c1">1</span>)       <span
                class="pl-c"><span class="pl-c">//</span> 0</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">log2</span>(<span class="pl-c1">0</span>)       <span
                class="pl-c"><span class="pl-c">//</span> -Infinity</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">log2</span>(<span class="pl-k">-</span><span class="pl-c1">2</span>)      <span
                class="pl-c"><span class="pl-c">//</span> NaN</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">log2</span>(<span class="pl-c1">1024</span>)    <span
                class="pl-c"><span class="pl-c">//</span> 10</span>
<span class="pl-c1">Math</span>.<span class="pl-c1">log2</span>(<span class="pl-c1">1</span> <span
                class="pl-k">&lt;&lt;</span> <span class="pl-c1">29</span>) <span class="pl-c"><span
                class="pl-c">//</span> 29</span></pre>
    </div>
    <p>对于没有部署这个方法的环境，可以用下面的代码模拟。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-smi">log2</span> <span class="pl-k">=</span> <span class="pl-c1">Math</span>.<span class="pl-smi">log2</span> <span
            class="pl-k">||</span> <span class="pl-k">function</span>(<span class="pl-smi">x</span>) {
  <span class="pl-k">return</span> <span class="pl-c1">Math</span>.<span class="pl-c1">log</span>(x) <span class="pl-k">/</span> <span
                class="pl-c1">Math</span>.<span class="pl-c1">LN2</span>;
};</pre>
    </div>
    <h3><a href="#双曲函数方法"  class="anchor" id="user-content-双曲函数方法">

    </a>双曲函数方法
    </h3>
    <p>ES6 新增了 6 个双曲函数方法。</p>
    <ul>
        <li><code>Math.sinh(x)</code> 返回<code>x</code>的双曲正弦（hyperbolic sine）</li>
        <li><code>Math.cosh(x)</code> 返回<code>x</code>的双曲余弦（hyperbolic cosine）</li>
        <li><code>Math.tanh(x)</code> 返回<code>x</code>的双曲正切（hyperbolic tangent）</li>
        <li><code>Math.asinh(x)</code> 返回<code>x</code>的反双曲正弦（inverse hyperbolic sine）</li>
        <li><code>Math.acosh(x)</code> 返回<code>x</code>的反双曲余弦（inverse hyperbolic cosine）</li>
        <li><code>Math.atanh(x)</code> 返回<code>x</code>的反双曲正切（inverse hyperbolic tangent）</li>
    </ul>
    <h2><a href="#mathsignbit"  class="anchor" id="user-content-mathsignbit">

    </a>Math.signbit()
    </h2>
    <p><code>Math.sign()</code>用来判断一个值的正负，但是如果参数是<code>-0</code>，它会返回<code>-0</code>。</p>
    <div class="highlight highlight-source-js">
        <pre><span class="pl-c1">Math</span>.<span class="pl-c1">sign</span>(<span class="pl-k">-</span><span
                class="pl-c1">0</span>) <span class="pl-c"><span class="pl-c">//</span> -0</span></pre>
    </div>
    <p>这导致对于判断符号位的正负，<code>Math.sign()</code>不是很有用。JavaScript 内部使用 64 位浮点数（国际标准 IEEE 754）表示数值，IEEE 754
        规定第一位是符号位，<code>0</code>表示正数，<code>1</code>表示负数。所以会有两种零，<code>+0</code>是符号位为<code>0</code>时的零值，<code>-0</code>是符号位为<code>1</code>时的零值。实际编程中，判断一个值是<code>+0</code>还是<code>-0</code>非常麻烦，因为它们是相等的。
    </p>
    <div class="highlight highlight-source-js">
        <pre><span class="pl-k">+</span><span class="pl-c1">0</span> <span class="pl-k">===</span> <span
                class="pl-k">-</span><span class="pl-c1">0</span> <span class="pl-c"><span
                class="pl-c">//</span> true</span></pre>
    </div>
    <p>目前，有一个<a href="http://jfbastien.github.io/papers/Math.signbit.html"
                rel="nofollow">提案</a>，引入了<code>Math.signbit()</code>方法判断一个数的符号位是否设置了。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span
            class="pl-en">signbit</span>(<span class="pl-c1">2</span>) <span class="pl-c"><span class="pl-c">//</span>false</span>
<span class="pl-c1">Math</span>.<span class="pl-en">signbit</span>(<span class="pl-k">-</span><span
                class="pl-c1">2</span>) <span class="pl-c"><span class="pl-c">//</span>true</span>
<span class="pl-c1">Math</span>.<span class="pl-en">signbit</span>(<span class="pl-c1">0</span>) <span
                class="pl-c"><span class="pl-c">//</span>false</span>
<span class="pl-c1">Math</span>.<span class="pl-en">signbit</span>(<span class="pl-k">-</span><span
                class="pl-c1">0</span>) <span class="pl-c"><span class="pl-c">//</span>true</span></pre>
    </div>
    <p>可以看到，该方法正确返回了<code>-0</code>的符号位是设置了的。</p>
    <p>该方法的算法如下。</p>
    <ul>
        <li>如果参数是<code>NaN</code>，返回<code>false</code></li>
        <li>如果参数是<code>-0</code>，返回<code>true</code></li>
        <li>如果参数是负值，返回<code>true</code></li>
        <li>其他情况返回<code>false</code></li>
    </ul>
    <h2><a href="#指数运算符"  class="anchor" id="user-content-指数运算符">

    </a>指数运算符
    </h2>
    <p>ES2016 新增了一个指数运算符（<code>**</code>）。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">2</span> <span class="pl-k">**</span> <span
            class="pl-c1">2</span> <span class="pl-c"><span class="pl-c">//</span> 4</span>
<span class="pl-c1">2</span> <span class="pl-k">**</span> <span class="pl-c1">3</span> <span class="pl-c"><span
                class="pl-c">//</span> 8</span></pre>
    </div>
    <p>指数运算符可以与等号结合，形成一个新的赋值运算符（<code>**=</code>）。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">let</span> a <span class="pl-k">=</span> <span
            class="pl-c1">1.5</span>;
a <span class="pl-k">*</span><span class="pl-k">*=</span> <span class="pl-c1">2</span>;
<span class="pl-c"><span class="pl-c">//</span> 等同于 a = a * a;</span>

<span class="pl-k">let</span> b <span class="pl-k">=</span> <span class="pl-c1">4</span>;
b <span class="pl-k">*</span><span class="pl-k">*=</span> <span class="pl-c1">3</span>;
<span class="pl-c"><span class="pl-c">//</span> 等同于 b = b * b * b;</span></pre>
    </div>
    <p>注意，在 V8 引擎中，指数运算符与<code>Math.pow</code>的实现不相同，对于特别大的运算结果，两者会有细微的差异。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-c1">Math</span>.<span class="pl-c1">pow</span>(<span
            class="pl-c1">99</span>, <span class="pl-c1">99</span>)
<span class="pl-c"><span class="pl-c">//</span> 3.697296376497263e+197</span>

<span class="pl-c1">99</span> <span class="pl-k">**</span> <span class="pl-c1">99</span>
<span class="pl-c"><span class="pl-c">//</span> 3.697296376497268e+197</span></pre>
    </div>
    <p>上面代码中，两个运算结果的最后一位有效数字是有差异的。</p>
    <h2><a href="#integer-数据类型"  class="anchor" id="user-content-integer-数据类型">

    </a>Integer 数据类型
    </h2>
    <h3><a href="#简介"  class="anchor" id="user-content-简介">

    </a>简介
    </h3>
    <p>JavaScript 所有数字都保存成 64 位浮点数，这决定了整数的精确程度只能到 53 个二进制位。大于这个范围的整数，JavaScript 是无法精确表示的，这使得 JavaScript
        不适合进行科学和金融方面的精确计算。</p>
    <p>现在有一个<a href="https://github.com/tc39/proposal-bigint">提案</a>，引入了新的数据类型
        Integer（整数），来解决这个问题。整数类型的数据只用来表示整数，没有位数的限制，任何位数的整数都可以精确表示。</p>
    <p>为了与 Number 类型区别，Integer 类型的数据必须使用后缀<code>n</code>表示。</p>
    <div class="highlight highlight-source-js">
        <pre><span class="pl-ii">1n</span> <span class="pl-k">+</span> <span class="pl-ii">2n</span> <span class="pl-c"><span
                class="pl-c">//</span> 3n</span></pre>
    </div>
    <p>二进制、八进制、十六进制的表示法，都要加上后缀<code>n</code>。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-ii">0b1101n</span> <span class="pl-c"><span
            class="pl-c">//</span> 二进制</span>
<span class="pl-ii">0o777n</span> <span class="pl-c"><span class="pl-c">//</span> 八进制</span>
<span class="pl-ii">0xFFn</span> <span class="pl-c"><span class="pl-c">//</span> 十六进制</span></pre>
    </div>
    <p><code>typeof</code>运算符对于 Integer 类型的数据返回<code>integer</code>。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">typeof</span> <span class="pl-ii">123n</span>
<span class="pl-c"><span class="pl-c">//</span> 'integer'</span></pre>
    </div>
    <p>JavaScript 原生提供<code>Integer</code>对象，用来生成 Integer 类型的数值。转换规则基本与<code>Number()</code>一致。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-en">Integer</span>(<span
            class="pl-c1">123</span>) <span class="pl-c"><span class="pl-c">//</span> 123n</span>
<span class="pl-en">Integer</span>(<span class="pl-s"><span class="pl-pds">'</span>123<span
                class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> 123n</span>
<span class="pl-en">Integer</span>(<span class="pl-c1">false</span>) <span class="pl-c"><span class="pl-c">//</span> 0n</span>
<span class="pl-en">Integer</span>(<span class="pl-c1">true</span>) <span class="pl-c"><span
                class="pl-c">//</span> 1n</span></pre>
    </div>
    <p>以下的用法会报错。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-k">new</span> <span
            class="pl-en">Integer</span>() <span class="pl-c"><span class="pl-c">//</span> TypeError</span>
<span class="pl-en">Integer</span>(<span class="pl-c1">undefined</span>) <span class="pl-c"><span class="pl-c">//</span>TypeError</span>
<span class="pl-en">Integer</span>(<span class="pl-c1">null</span>) <span class="pl-c"><span class="pl-c">//</span> TypeError</span>
<span class="pl-en">Integer</span>(<span class="pl-s"><span class="pl-pds">'</span>123n<span
                class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> SyntaxError</span>
<span class="pl-en">Integer</span>(<span class="pl-s"><span class="pl-pds">'</span>abc<span
                class="pl-pds">'</span></span>) <span class="pl-c"><span class="pl-c">//</span> SyntaxError</span></pre>
    </div>
    <h3><a href="#运算"  class="anchor" id="user-content-运算">

    </a>运算
    </h3>
    <p>在数学运算方面，Integer 类型的<code>+</code>、<code>-</code>、<code>*</code>和<code>**</code>这四个二元运算符，与 Number
        类型的行为一致。除法运算<code>/</code>会舍去小数部分，返回一个整数。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-ii">9n</span> <span class="pl-k">/</span> <span
            class="pl-ii">5n</span>
<span class="pl-c"><span class="pl-c">//</span> 1n</span></pre>
    </div>
    <p>几乎所有的 Number 运算符都可以用在 Integer，但是有两个除外：不带符号的右移位运算符<code>
        &gt;&gt;&gt;</code>和一元的求正运算符<code>+</code>，使用时会报错。前者是因为<code>&gt;&gt;&gt;</code>要求最高位补 0，但是 Integer
        类型没有最高位，导致这个运算符无意义。后者是因为一元运算符<code>+</code>在 asm.js 里面总是返回 Number 类型或者报错。</p>
    <p>Integer 类型不能与 Number 类型进行混合运算。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-ii">1n</span> <span class="pl-k">+</span> <span
            class="pl-c1">1</span>
<span class="pl-c"><span class="pl-c">//</span> 报错</span></pre>
    </div>
    <p>这是因为无论返回的是 Integer 或 Number，都会导致丢失信息。比如<code>(2n**53n + 1n) + 0.5</code>这个表达式，如果返回 Integer 类型，<code>0.5</code>这个小数部分会丢失；如果返回
        Number 类型，会超过 53 位精确数字，精度下降。</p>
    <p>相等运算符（<code>==</code>）会改变数据类型，也是不允许混合使用。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-ii">0n</span> <span class="pl-k">==</span> <span
            class="pl-c1">0</span>
<span class="pl-c"><span class="pl-c">//</span> 报错 TypeError</span>

<span class="pl-ii">0n</span> <span class="pl-k">==</span> <span class="pl-c1">false</span>
<span class="pl-c"><span class="pl-c">//</span> 报错 TypeError</span></pre>
    </div>
    <p>精确相等运算符（<code>===</code>）不会改变数据类型，因此可以混合使用。</p>
    <div class="highlight highlight-source-js"><pre><span class="pl-ii">0n</span> <span class="pl-k">===</span> <span
            class="pl-c1">0</span>
<span class="pl-c"><span class="pl-c">//</span> false</span></pre>
    </div>
</div>
</body>
</html>
